import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
@Injectable({
  providedIn: 'root',
})
export class ReviewRolesService {
  data = '/Approval';
  constructor(private http: HttpClient) {}
  private createPaginationParams(
    currentPage: number,
    itemsPerPage: number
  ): HttpParams {
    return new HttpParams()
      .set('pageNumber', currentPage.toString())
      .set('pageSize', itemsPerPage.toString());
  } // 创建分页参数
  GetPagedApprovalGroup(currentPage: number, itemsPerPage: number) {
    const GetAllCodeRules = '/GetApprovalGroupsPage';
    const params = this.createPaginationParams(currentPage, itemsPerPage);
    return this.http.get<any>(
      `${environment.apiUrl}${this.data}${GetAllCodeRules}`,
      { params }
    );
  } // 获取所有审批组
  DeleteApprovalGroup(id: string) {
    const HardDeleteCodeRule = '/DeleteApprovalGroup';
    return this.http.delete(
      environment.apiUrl + this.data + HardDeleteCodeRule,
      { params: { id } }
    );
  } // 删除编码规则
  CreateApprovalGroup(data: any) {
    const CreateApprovalGroup = '/CreateApprovalGroup';
    return this.http.post(
      environment.apiUrl + this.data + CreateApprovalGroup,
      data
    );
  } // 创建审批组
  UpdateApprovalGroup(id: any, data: any) {
    const UpdateApprovalGroup = '/UpdateApprovalGroup';
    return this.http.put(
      environment.apiUrl + this.data + UpdateApprovalGroup,
      data,
      { params: { id } }
    );
  } // 修改审批组
  GetApprovalGroupById(id: any) {
    const GetApprovalGroupById = '/GetApprovalGroupById';
    return this.http.get(
      environment.apiUrl + this.data + GetApprovalGroupById,
      { params: { id } }
    );
  } // 获取审批组
  GetApprovalFlowConfigs(currentPage: number, itemsPerPage: number) {
    const GetApprovalFlowConfigs = '/GetApprovalFlowConfigs';
    const params = this.createPaginationParams(currentPage, itemsPerPage);
    return this.http.get(
      environment.apiUrl + this.data + GetApprovalFlowConfigs,
      { params }
    );
  } // 获取审批流程配置
  CreateApprovalFlow(data: any) {
    const CreateApprovalFlow = '/CreateApprovalFlow';
    const headers = new HttpHeaders().set('Content-Type', 'application/json');
    return this.http.post(
      environment.apiUrl + this.data + CreateApprovalFlow,
      data,
      { headers }
    );
  } // 创建审批流程
  DeleteApprovalFlowConfigRange(id: any) {
    const options = {
      body: id,
      responseType: 'text' as 'json',
    };
    const DeleteApprovalFlowConfigRange = '/DeleteApprovalFlowConfigRange';
    return this.http.delete(
      environment.apiUrl + this.data + DeleteApprovalFlowConfigRange,
      options
    );
  } // 删除审批流程配置
  UpdateApprovalFlowConfig(id: any, data: any) {
    const UpdateApprovalFlowConfig = '/UpdateApprovalFlowConfig';
    return this.http.put(
      environment.apiUrl + this.data + UpdateApprovalFlowConfig,
      data,
      { params: { id } }
    );
  } // 修改审批流程配置
  DesignApprovalFlow(data: any) {
    const DesignApprovalFlow = '/DesignApprovalFlow';
    return this.http.put(
      environment.apiUrl + this.data + DesignApprovalFlow,
      data,
      { responseType: 'text' }
    );
  } // 设计审批流程
  GetApprovalFlowConfig(id: any) {
    const GetApprovalFlowConfig = '/GetApprovalFlowConfig';
    return this.http.get(
      environment.apiUrl + this.data + GetApprovalFlowConfig,
      { params: { id } }
    );
  } // 获取审批流程配置
  StartApprovalFlow(flowName: any, data: any) {
    const StartApprovalFlow = '/StartApprovalFlow';
    return this.http.post(environment.apiUrl + this.data +StartApprovalFlow, data, {
      params: { flowName },
      responseType: 'text'
    });
  }
  ChangeApprovalFlowConfig(id: any, enable: any) {
    const ChangeApprovalFlowConfig = '/ChangeApprovalFlowConfig';
    return this.http.put(
      environment.apiUrl + this.data + ChangeApprovalFlowConfig,null,
      { params: { id, enable } }
    );
  }
  GetApprovalNodeInstancesPaged(Status: any, SourceNum: any, PageNumber: any, PageSize: any) {
    const GetApprovalNodeInstancesPaged = '/GetApprovalNodeInstancesPaged';
    return this.http.get(
      environment.apiUrl + this.data + GetApprovalNodeInstancesPaged,
      { params: { Status, SourceNum, PageNumber, PageSize } }
    );
  }// 获取审批节点实例
  GetApprovalNodeInstancesPaged2({
    SourceNum,
    PageNumber,
    PageSize,
  }) {
    const GetApprovalNodeInstancesPaged = '/GetApprovalNodeInstancesPaged';
    return this.http.get(
      environment.apiUrl + this.data + GetApprovalNodeInstancesPaged,
      { params: { SourceNum, PageNumber, PageSize } }
    );
  }// 获取审批节点实例
  ApproveNode(approvalNodeInstanceId: any, num: any) {
    const ApproveNode = '/ApproveNode';
    return this.http.post(
      environment.apiUrl + this.data + ApproveNode,null,
      { params: { approvalNodeInstanceId, num },
       responseType: 'text' }
    );
  }// 审批节点
  RejectNode(approvalNodeInstanceId: any, num: any, reason: any) {
    const RejectNode = '/RejectNode';
    return this.http.post(
      environment.apiUrl + this.data + RejectNode,null,
      { params: { approvalNodeInstanceId, num, reason },
       responseType: 'text' }
    );
  }// 驳回节点
  GetAuditPagedLogsByNumber(Num: any, PageNumber: any, PageSize: any) {
    const GetAuditPagedLogsByNumber = '/GetAuditPagedLogsByNumber';
    return this.http.get(
      environment.apiUrl + this.data + GetAuditPagedLogsByNumber,
      { params: { Num, PageNumber, PageSize } }
    );
  }// 获取审批日志
}
